﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IPopupService PopupService



<div class="@Class">
    <MRow>
        @foreach(var item in Model)
        {
            <MCol Cols="12" Md="6" Lg="4">
                <CnGalWebSite.Components.Inputs.MasaAutoComplete @bind-Value="@item.DisplayName" Label="标签" AutoCompleteItems="AutoCompleteItems" />
            </MCol>
        }
</MRow>
    <div>
        <CnGalWebSite.Components.Buttons.MasaButton Text="添加" OnClick="OnAdd" Icon="mdi-plus" Rounded/>
        <CnGalWebSite.Components.Buttons.MasaButton Text="创建" OnClick="OnCreate" Icon="mdi-creation" Class="ms-4" Rounded/>
    </div>
</div>

@code {
    [Parameter]
    public List<RelevancesModel> Model { get; set; }
    [Parameter]
    public List<string> AutoCompleteItems { get; set; }
    [Parameter]
    public string Class { get; set; }
    [Parameter]
    public EventCallback<string> OnInput { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            await Task.Delay(500);
            await OnInput.InvokeAsync();
        }
    }

    private async Task OnAdd()
    {
        try
        {
            Model.Add(new RelevancesModel());
            await OnInput.InvokeAsync();
            StateHasChanged();
        }
        catch
        {
            await PopupService.ToastAsync("添加标签失败", AlertTypes.Error);
        }
    }

    public void Refresh()
    {
        StateHasChanged();
        

    }

    public async Task OnValueChanged(int index, string text)
    {
        Model[index].DisplayName = text;
        await OnInput.InvokeAsync();
        StateHasChanged();
    }

    public async Task OnCreate()
    {
        var ret = await PopupService.ConfirmAsync("确认跳转标签创建页面", "跳转后当前页面数据不会保存，请确保备份编辑数据后跳转");

        if (ret == false)
        {
            return;
        }
        NavigationManager.NavigateTo( "/tags/createtag");
    }

}
